import 'bootstrap/dist/css/bootstrap.css';
import '../css/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.min.css';
import '../css/index2.scss';

require('bootstrap/dist/js/bootstrap.js');
require('./common.js');
$('#sidebar-menu a[href="website_board.html"]').addClass('active')
$('#sidebar-menu a[href="website_board.html"]').parent('li').addClass('active')

require('./plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.min.js');

require('./plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.zh-CN.js');

import { websiteDetailUrl} from './urlconfig/config.js';

import {tableInit} from './tableFunction/cmmon.js';


let name = getQueryString("web_name"), datetime = '';
let reqData = {
	website_name:name,
	type: 'rise',
	time: '' 
};
let dropReqData = {
	website_name:name,
	type: 'drop',
	time: '' 
};



function getQueryString(name) { 
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
		var r = window.location.search.substr(1).match(reg); 
		if (r != null) return decodeURIComponent(r[2]); return null; 
	}
$.extend(window, {
    operateFormatter: function(value, row) {
    	let link = 'key_detail.html?key_word='+row.key_name;
    	return '<a class="btn btn-default btn-sm" href="'+link+'">查看明细</a>'
    }
});
let columns = [
			{
                field: 'key_name',
                title: '关键词'
            },{
                field: 'old_rank',
                title: '原排名',
            },{
            	field: 'new_rank',
                title: '新排名',
            },{
                field: 'all_expoment',
                title: '整体指数'
            },{
                field: 'pc_expoment',
                title: 'pc指数',
                
            },{
                field: 'mobile_expoment',
                title: '移动指数',
            },{
            	field: 'name360_expoment',
            	title: '360指数',
            	
            }, {
            	field: 'operate',            	
            	formatter: operateFormatter
            }
		];
let baseConfig = {
	list: {
		url: websiteDetailUrl,
		type: 'post',
		data: reqData
	},
	table: {
		id: '#datatable-editable',
		columns: columns	
	},	
	//分页默认是bootstrap																	
	page: false, //是否需要分页
};





let dropConfig = {
	list: {
		url: websiteDetailUrl,
		type: 'post',
		data: dropReqData
	},
	table: {
		id: '#datatable',	
		columns: columns
	},	
	//分页默认是bootstrap																	
	page: false, //是否需要分页
}
var table = new tableInit(baseConfig);


var table2 = new tableInit(dropConfig);

$('.page-title').html(name+'关键词涨跌明细')	

function loadError() {

}

// var table2 = new tableInit(dropConfig);
$("#startTime").datetimepicker({
	language: 'zh-CN',
	todayBtn:  1,
	autoclose: 1,
	minView: 2,
	todayHighlight: 1,
	endDate: new Date()
}).on('changeDate', function() {
	datetime = $(this).val().replace(/\s/ig,'');
		$('.nav-tabs li[role="presentation"]').each(function() {
		if($(this).hasClass('active')) {
			let type = $(this).attr('data-type');
			
			if(type == 'rise') {
				reqData.time = datetime;
				$.ajax({
					url: baseConfig.list.url,
			  		type: baseConfig.list.type,
			  		dataType:'JSON',
			  		data: baseConfig.list.data,
			  		success:function(res) {
			  			if(res.status === 1) {
			  				table.$table.bootstrapTable('load', res.data.items)
			  			}else {
			  				
			  				table.$table.bootstrapTable('load', [])
			  			}
			  			console.log(res)
			  		},
			  		error: function() {
			  			table.$table.bootstrapTable('load', [])
			  		}
				})
			}else {
				dropReqData.time = datetime;
				$.ajax({
					url: dropConfig.list.url,
			  		type: dropConfig.list.type,
			  		dataType:'JSON',
			  		data: dropConfig.list.data,
			  		success:function(res) {
			  			if(res.status === 1) {
			  				table2.$table.bootstrapTable('load', res.data.items)
			  			}else {
			  				table.$table.bootstrapTable('load', [])
			  			}
			  			console.log(res)
			  		}
				})
			}
		}else {
			let type = $(this).attr('data-type');
			
			if(type == 'rise') {
				reqData.time = datetime;
				$.ajax({
					url: baseConfig.list.url,
			  		type: baseConfig.list.type,
			  		dataType:'JSON',
			  		data: baseConfig.list.data,
			  		success:function(res) {
			  			if(res.status === 1) {
			  				table.$table.bootstrapTable('load', res.data.items)
			  			}else {
			  				table.$table.bootstrapTable('load', [])
			  			}
			  			console.log(res)
			  		}
				})
			}else {
				dropReqData.time = datetime;
				$.ajax({
					url: dropConfig.list.url,
			  		type: dropConfig.list.type,
			  		dataType:'JSON',
			  		data: dropConfig.list.data,
			  		success:function(res) {
			  			if(res.status === 1) {
			  				table2.$table.bootstrapTable('load', res.data.items)
			  			}else {
			  				table.$table.bootstrapTable('load', [])
			  			}
			  			console.log(res)
			  		}
				})
			}
		}
		
	})
})


$("#comparison a").click(function(e) {
	let _self = $(this), link, type = 'rise', web_name = getQueryString("web_name");
	e.preventDefault();
	$('.nav-tabs li[role="presentation"]').each(function() {
		if($(this).hasClass('active')) {
			type = $(this).attr('data-type');
		}
	})

	let date = datetime;
	if(datetime.length == 0) {
		date = '';
	}
	link = 'website_comparison.html?type='+type+'&date='+date+'&web_name='+ web_name;
	window.location.href = link;

})






